<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_AuthController extends Zend_Controller_Action 
{
	

	
	public function preDispatch()
	{
		
		//$this->_helper->layout->setLayout('common');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		//$this->getHelper('User')->getinfo();
		
	}
	
	public function confirmcodeAction(){
		
		$this->getHelper ('layout')->disableLayout ();
		
		$this->_helper->viewRenderer->setNoRender();
		
		$this->view->confirmCode('administratorConfirmCode');
		
	}
	
	public function errorAction(){
		
		$this->_helper->viewRenderer->setNoRender();
		
		$this->view->showMsg("您已经登陆了!");
		
	}

	public function loginAction(){
		
		$member=new CMember();
		
		$this->view->assign('current_action','login');
		
		if($member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			$this->_forward('error');
			
			return false;
			
		}
		
		return true;
		
	}

	
	public function dologinAction(){
		
		$this->_helper->viewRenderer->setNoRender();
		
		if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		
		Zend_Session::start();
		
		$username=$this->_request->getParam("username");
		
		$userpass=$this->_request->getParam("userpass");
		
		$useryz=$this->_request->getParam("useryz");

		if($username==''||$userpass==''||$useryz==''){
			
			$this->view->showMsg("请填写完整登陆信息!");
			
			return false;
			
		}
		
		if(strtolower($useryz)!=strtolower($_SESSION['administratorConfirmCode'])){
			
			$this->view->showMsg("验证码错误!");
			
			return false;
			
		}

		$member=new CMember();
		
		$thisArray=$member->login($username,$userpass,'administrator');
		
		if(count($thisArray)==0){
			
			$this->view->showMsg("请检查您的用户名和密码!");
			
			return false;
			
		}
		
		$this->setCookie($thisArray['uid'],$thisArray['username'],$thisArray['userpass'],0,$thisArray['member_type']);
		
		$this->view->showMsg("管理员登陆成功!",'administrator');
		
		return true;
		
	}
	

	
    public function logoutAction() 
    {
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	Zend_Session::start();
        
    	session_unset();
    	
		session_destroy();
		
		setcookie(COOKIE_PRE.'uid','',-10,'/');
    			
    	setcookie(COOKIE_PRE.'username','',-10,'/');
    	
    	setcookie(COOKIE_PRE.'userpass','',-10,'/');
    	
    	setcookie(COOKIE_PRE.'member_type','',-10,'/');
    	
    	setcookie(COOKIE_PRE.'member_admin','',-10,'/');
    	
    	$this->view->showMsg("退出成功!",$_SERVER['HTTP_REFERER']);
    	
    }
    
    public function setCookie($uid,$username,$userpass,$is_enterprice,$member_type){

    	setcookie(COOKIE_PRE.'uid',$uid,null,'/');
		setcookie(COOKIE_PRE.'username',$username,null,'/');
		setcookie(COOKIE_PRE.'userpass',$userpass,null,'/');
		setcookie(COOKIE_PRE.'is_enterprice',$is_enterprice,null,'/');
		setcookie(COOKIE_PRE.'member_type',$member_type,null,'/');
		setcookie(COOKIE_PRE.'member_admin',$this->_request->getModuleName(),null,'/');
		
		$sellerSession=new Zend_Session_Namespace('member');

		$sellerSession->userCode=$userpass;
		$sellerSession->userpass=$userpass;
		$sellerSession->uid=$uid;
		$sellerSession->member_type=$member_type;
    	
    }

    
}
